package com.june.springmvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.june.springmvc.pojo.User;
import com.june.springmvc.service.UserService;

@Controller
@Scope("prototye")
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;

	//增、改
	@RequestMapping({"/save","/update"})
	@ResponseBody
	public Object save(User user) {
		Map<String, Object> result = new HashMap<>();
		try {
			userService.save(user);
			result.put("message", "保存成功!");
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("message", "保存成功!");
			result.put("success", false);
		}
		return result;
	}
	
	//删
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(User user) {
		Map<String, Object> result = new HashMap<>();
		try {
			userService.delete(user);
			result.put("message", "删除成功!");
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("message", "删除成功!");
			result.put("success", false);
		}
		return result;
	}
	
	//批量删除
	@RequestMapping("/batchDelete")
	@ResponseBody
	public Object batchDelete(String ids) {
		Map<String, Object> result = new HashMap<>();
		try {
			userService.batchDelete(ids.split(","));
			result.put("message", "删除成功!");
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("message", "删除成功!");
			result.put("success", false);
		}
		return result;
	}
	
	//查
	@RequestMapping("/findOne")
	@ResponseBody
	public Object findOne(User user) {
		Map<String, Object> result = new HashMap<>();
		try {
			User data = userService.findOne(user);
			result.put("message", "获取成功!");
			result.put("success", true);
			result.put("data", data);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("message", "获取成功!");
			result.put("success", false);
		}
		return result;
	}
	
	//查全部
	@RequestMapping("/findAll")
	@ResponseBody
	public Object findAll() {
		Map<String, Object> result = new HashMap<>();
		try {
			List<User> users = userService.findPageData(null,null,null);
			result.put("message", "获取成功!");
			result.put("success", true);
			result.put("data", users);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("message", "获取成功!");
			result.put("success", false);
		}
		return result;
	}
	
	//分页查
	@RequestMapping("/pageQuery")
	@ResponseBody
	public Object pageQuery(User user,int page,int rows) {
		Map<String, Object> result = new HashMap<>();
		try {
			List<User> pageData = userService.findPageData(user, page, rows);
			Map<String, Object> data = new HashMap<>();
			data.put("total", userService.selectCount(user));
			data.put("rows", pageData);
			result.put("message", "删除成功!");
			result.put("success", true);
			result.put("data", data);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("message", "删除成功!");
			result.put("success", false);
		}
		return result;
	}
}